package com.codi.bus.core.service.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.codi.base.domain.BaseDomain;
import com.codi.base.util.DateUtils;
import com.codi.base.util.MoneyUtils;
import com.codi.base.util.StringUtil;
import com.codi.bus.constant.FundBusinCodeUtil;
import com.codi.bus.constant.GlobalConstant;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.Date;

public class ApplyModel extends BaseDomain {


    /**
     * 手续费
     */
    @Getter
    @Setter
    private BigDecimal expense;

    /**
     * 展示  申购或者赎回文案
     */
    @Getter
    @Setter
    private String expenseTtile;
    private FundApplyType fundApplyType = FundApplyType.Normal;
    @Getter
    @Setter
    private Boolean isOrg = false; // 是否是机构
    // 基金代码
    @Getter
    @Setter
    private String fundCode;
    // 基金名称
    @Getter
    @Setter
    private String fundName;
    // 申请编号
    @Getter
    @Setter
    private String allotNo;
    // 下单日期
    private String orderDate;
    // 下单时间
    private String orderTime;
    // 申购金额
    private BigDecimal balance;
    // 赎回份额
    private BigDecimal shares;
    // 银行代码
    private String bankCode;
    // 银行恒生代码
    private String bankHSCode;
    // 银行名称
    private String bankName;
    // 扣款状态
    private String deductStatus;
    @Getter
    @Setter
    private String capitalMode;
    // 确认标志
    private String taConfirmFlag;
    // 业务代码
    private String fundBusinCode;
    //赎回转购的业务代码 ：43
    //业务辅助代码
    @Getter
    @Setter
    private String businAssCode;
    //业务辅助名称
    @Getter
    @Setter
    private String businAssName;
    //原申请编号  赎回转购的申请 指向 赎回
    @Getter
    @Setter
    private String originalAppno;
    // 业务名称从数据库Lookup表中获取
    private String fundBusinName;
    // 交易账号
    private String tradeAcco;
    // 发送状态
    private String sendFlag;
    // 申请日期（15:00之后，申请日期为T+1)
    private String applyDate;
    @Getter
    @Setter
    private String portfolioAllotNo;
    @Getter
    @Setter
    private String portfolioCode;
    @Getter
    @Setter
    private String extPortfolioCode;
    @Getter
    @Setter
    private String protocolName;
    @Getter
    @Setter
    private String statusString;
    @Getter
    @Setter
    private Boolean isTransfer;
    //回款账户
    @Getter
    @Setter
    private String receivableAccount;
    // 银行卡号
    @Setter
    @Getter
    private String bankAccount;
    //赎回转购目标基金
    @Getter
    @Setter
    private String targetFundCode;

    /**
     * 手续费字符串
     */
    public String getExpenseStr() {
        if (this.expense == null) {
            if (GlobalConstant.TACONFIRM_FLAG_UNCONFIRM.equals(getTAConfirmFlag())) {
                return "-.--";
            }
            return "0.00";
        }
        if (this.expense.compareTo(BigDecimal.valueOf(0)) == 0 && GlobalConstant.TACONFIRM_FLAG_UNCONFIRM.equals(getTAConfirmFlag())) {
            return "-.--";
        }
        if (this.expense.compareTo(BigDecimal.valueOf(0)) == 0) {
            return "0.00";
        }
        return MoneyUtils.formatMoney(this.expense, 2);
    }

    public void setFundType(FundApplyType fundApplyType) {
        Assert.notNull(fundApplyType);
        this.fundApplyType = fundApplyType;
    }

    public String getFundName() {
        return this.fundName;
    }

    public void setFundName(String fundName) {
        this.fundName = fundName;
    }

    public String getOrderDate() {
        if (!StringUtil.isEmpty(orderDate)) {
            Date date = DateUtils.stringToDate(orderDate, GlobalConstant.DATE_FORMAT_YYYYMMDD);
            return DateUtils.dateToSimpleStr(date);
        }
        return this.orderDate;
    }

    public void setOrderDate(String orderDate) {
        this.orderDate = orderDate;
    }

    @JSONField(serialize = false)
    public String getOrderDateReal() {
        return this.orderDate;
    }

    public String getOrderTime() {
        if (!StringUtil.isEmpty(orderTime)) {
            Date date = DateUtils.stringToDate(orderTime, GlobalConstant.DATE_FORMAT_HHMMSS);
            return DateUtils.dateToTime(date);
        }
        return this.orderTime;
    }

    public void setOrderTime(String orderTime) {
        this.orderTime = orderTime;
    }

    //避免提前序列化
    @JSONField(serialize = false)
    public String getOrderTimeReal() {
        return this.orderTime;
    }

    public BigDecimal getBalance() {
        return this.balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

    // 申购金额字符串
    public String getBalanceString() {
        if (this.balance.compareTo(BigDecimal.valueOf(0)) == 0) {
            return "-.--";
        }
        return MoneyUtils.formatMoney(balance, 2);
    }

    public BigDecimal getShares() {
        return this.shares;
    }

    public void setShares(BigDecimal shares) {
        this.shares = shares;
    }

    // 赎回份额字符串
    public String getSharesString() {
        if (this.shares.compareTo(BigDecimal.valueOf(0)) == 0) {
            return "-.--";
        }
        return MoneyUtils.formatMoney(this.shares, 2);
    }

    public String getBankCode() {
        return this.bankCode;
    }

    public void setBankCode(String bankCode) {
        this.bankCode = bankCode;
    }

    public String getBankHSCode() {
        return this.bankHSCode;
    }

    public void setBankHSCode(String bankHSCode) {
        this.bankHSCode = bankHSCode;
    }
//    //针对赎回转购前的基金代码
//    @Getter
//    @Setter
//    private String assFundCode;

    public String getBankName() {
        return this.bankName;
    }

    public void setBankName(String bankName) {
        this.bankName = bankName;
    }

    public String getDeductStatus() {
        return this.deductStatus;
    }

    public void setDeductStatus(String deductStatus) {
        this.deductStatus = deductStatus;
    }

    // 扣款状态名称
    public String getDeductStatusString() {
        if (this.fundBusinCode.equals(GlobalConstant.FUND_BUSIN_CODE_APPLY_BUY) || getIsFixBuy()) {
            if (this.deductStatus.equals(GlobalConstant.DEDUCT_STATUS_UNCHECK)) {
                if (isOrg || capitalMode == "K") {
                    return GlobalConstant.DEDUCT_STATUS_UNCHECK_ORG_STRING;
                }
                return GlobalConstant.DEDUCT_STATUS_UNCHECK_STRING;
            }
            if (this.deductStatus.equals(GlobalConstant.DEDUCT_STATUS_INVALID)) {
                if (isOrg || capitalMode == "K") {
                    return GlobalConstant.DEDUCT_STATUS_INVALID_ORG_STRING;
                }
                return GlobalConstant.DEDUCT_STATUS_INVALID_STRING;
            }
            if (this.deductStatus.equals(GlobalConstant.DEDUCT_STATUS_VALID)) {
                if (isOrg || capitalMode == "K") {
                    return GlobalConstant.DEDUCT_STATUS_VALID_ORG_STRING;
                }
                return GlobalConstant.DEDUCT_STATUS_VALID_STRING;
            }
            if (this.deductStatus.equals(GlobalConstant.DEDUCT_STATUS_SENT)) {
                return GlobalConstant.DEDUCT_STATUS_SENT_STRING;
            }
        }

        if (this.fundBusinCode.equals(GlobalConstant.FUND_BUSIN_CODE_APPLY_SELL) || getIsFixSell()) {
            if (this.deductStatus.equals(GlobalConstant.DEDUCT_STATUS_VALID)) {
                if (this.taConfirmFlag.equals(GlobalConstant.TACONFIRM_FLAG_UNCONFIRM)) {
                    if (fundApplyType == FundApplyType.T0) {
                        return "交易中";
                    }
                    return GlobalConstant.DEDUCT_STATUS_SELLING_STRING;
                }

                if (this.taConfirmFlag.equals(GlobalConstant.TACONFIRM_FLAG_CONFIRMED)) {
                    return GlobalConstant.DEDUCT_STATUS_SELLED_STRING;
                }
            }
        }

        return "";
    }

    /**
     * 是否可撤单状态位
     *
     * @return
     */
    public boolean getUndoStatus() {
        // 定投不能撤单
        if (getIsFixBuy() || getIsFixSell()) {
            return false;
        }
        if (GlobalConstant.PORTFOLIO_TRANSFER.equals(this.getFundBusinName())) {
            return false;
        }
        if (fundApplyType == FundApplyType.T0
            && GlobalConstant.FUND_BUSIN_CODE_T0_FAST_APPLY_SELL.equals(fundBusinCode)) {
            return false;
        }
        // 如果此申购交易已撤单，则不能再撤单
        if (this.taConfirmFlag.equals(GlobalConstant.TACONFIRM_FLAG_UNDO)) {
            return false;
        }
        // 如果是撤单申请交易，则不能再对撤单交易再撤单
        if (this.fundBusinCode.equals(GlobalConstant.FUND_BUSIN_CODE_APPLY_UNDO)) {
            return false;
        }
        // 15:00, 有效、未发送的交易才可以撤单
        Date date = DateUtils.stringToDate(
            MessageFormat.format("{0}{1}", this.applyDate, GlobalConstant.WORKING_DATE_END_TIME),
            DateUtils.yyyyMMddHHmmss_plain);
        Date now = new Date();

        if (this.isOrg) {
            if (GlobalConstant.FUND_BUSIN_CODE_APPLY_BUY.equals(this.fundBusinCode)){
            //机构不能撤单
            return false;
            }
//            if (this.deductStatus.equals(GlobalConstant.DEDUCT_STATUS_UNCHECK)
//                && this.sendFlag.equals(GlobalConstant.SEND_FLAG_UNSEND)
//                && now.before(date)) {
//                return true;
//            }
            if (this.deductStatus.equals(GlobalConstant.DEDUCT_STATUS_VALID)
                && this.sendFlag.equals(GlobalConstant.SEND_FLAG_UNSEND)
                && now.before(date)) {
                return true;
            }
        } else {
            if (this.deductStatus.equals(GlobalConstant.DEDUCT_STATUS_VALID)
                && this.sendFlag.equals(GlobalConstant.SEND_FLAG_UNSEND)
                && now.before(date)) {
                return true;
            }
        }

        return false;
    }

    /**
     * 是否是定投申购
     *
     * @return
     */

    public boolean getIsFixBuy() {
        return FundBusinCodeUtil.isFixBuy(this.fundBusinCode);
    }

    /**
     * 是否是定投赎回
     *
     * @return
     */
    public boolean getIsFixSell() {
        return FundBusinCodeUtil.isFixSell(this.fundBusinCode);
    }

    public String getTAConfirmFlag() {
        return this.taConfirmFlag;
    }

    public void setTAConfirmFlag(String taConfirmFlag) {
        this.taConfirmFlag = taConfirmFlag;
    }

    // 确认标志字符串
    public String getTAConfirmFlagString() {
        // 未确认且非无效扣款状态
        if (this.taConfirmFlag.equals(GlobalConstant.TACONFIRM_FLAG_UNCONFIRM)
            && !this.deductStatus.equals(GlobalConstant.DEDUCT_STATUS_INVALID)) {
            return GlobalConstant.TACONFIRM_FLAG_UNCONFIRM_STRING;
        }

        if (this.taConfirmFlag.equals(GlobalConstant.TACONFIRM_FLAG_ACTION)) {
            return GlobalConstant.TACONFIRM_FLAG_ACTION_STRING;
        }

        if (this.taConfirmFlag.equals(GlobalConstant.TACONFIRM_FLAG_CONFIRMED)) {
            if (fundApplyType == FundApplyType.T0) {
                return "交易成功";
            }
            return GlobalConstant.TACONFIRM_FLAG_CONFIRMED_STRING;
        }

        if (this.taConfirmFlag.equals(GlobalConstant.TACONFIRM_FLAG_FAIL)) {
            if (fundApplyType == FundApplyType.T0) {
                return "交易失败";
            }
            return GlobalConstant.TACONFIRM_FLAG_FAIL_STRING;
        }

        if (this.taConfirmFlag.equals(GlobalConstant.TACONFIRM_FLAG_PART_CONFIRMED)) {
            return GlobalConstant.TACONFIRM_FLAG_PART_CONFIRMED_STRING;
        }

        if (this.taConfirmFlag.equals(GlobalConstant.TACONFIRM_FLAG_REAL_SUCCESS)) {
            if (fundApplyType == FundApplyType.T0) {
                return "交易成功";
            }
            return GlobalConstant.TACONFIRM_FLAG_REAL_SUCCESS_STRING;
        }

        if (this.taConfirmFlag.equals(GlobalConstant.TACONFIRM_FLAG_UNDO)) {
            return GlobalConstant.TACONFIRM_FLAG_UNDO_STRING;
        }
        return "";
    }

    public String getFundBusinCode() {
        return this.fundBusinCode;
    }

    public void setFundBusinCode(String fundBusinCode) {
        this.fundBusinCode = fundBusinCode;
    }

    // 业务名称
    public String getFundBusinName() {
        if (fundApplyType == FundApplyType.T0) {
            // 赎回转购
            if (GlobalConstant.BUSIN_ASS_NAME_CURRENCY_SEEL_THEN_BUY.equals(this.getBusinAssName())) {
                return GlobalConstant.BUSIN_ASS_NAME_CURRENCY_SEEL_THEN_BUY;
            }
            if (GlobalConstant.FUND_BUSIN_CODE_APPLY_BUY.equals(this.fundBusinCode)) {
                return "存入";
            } else if (GlobalConstant.FUND_BUSIN_CODE_APPLY_FIX_BUY.equals(this.fundBusinCode)) {
                return "定投申购";
            } else if (FundBusinCodeUtil.isT0Sell(fundBusinCode)) {
                return "快速取现";
            } else if (FundBusinCodeUtil.isNormalSell(fundBusinCode)) {
                return "普通取现";
            } else if (FundBusinCodeUtil.isSell(fundBusinCode)) {
                return "取现";
            }
        }
        return this.fundBusinName;
    }

    public void setFundBusinName(String fundBusinName) {
        this.fundBusinName = fundBusinName;
    }


    // spy 2017-11-13 14:50:24

    @JSONField(serialize = false)
    public String getFundBusinNameReal() {
        return this.fundBusinName;
    }

    public String getTradeAcco() {
        return this.tradeAcco;
    }

    public void setTradeAcco(String tradeAcco) {
        this.tradeAcco = tradeAcco;
    }

    public String getSendFlag() {
        return this.sendFlag;
    }

    public void setSendFlag(String sendFlag) {
        this.sendFlag = sendFlag;
    }

    public String getApplyDate() {
        if (!StringUtil.isEmpty(applyDate)) {
            Date date = DateUtils.stringToDate(applyDate, GlobalConstant.DATE_FORMAT_YYYYMMDD);
            return DateUtils.dateToSimpleStr(date);
        }
        return this.applyDate;
    }

    public void setApplyDate(String applyDate) {
        this.applyDate = applyDate;
    }

    @JSONField(serialize = false)
    public String getApplyDateReal() {
        return this.applyDate;
    }

    /**
     * fund types
     */
    public static enum FundApplyType {
        Normal, T0;
    }

}
